import {Component, OnInit, Input, OnChanges, Output, EventEmitter, OnDestroy} from '@angular/core';
import {Router, NavigationEnd, ActivatedRoute} from '@angular/router';

@Component({
    selector: 'kylin-side-menu',
    templateUrl: './side-menu.component.html',
    styleUrls: ['./side-menu.component.less']
})
export class SideMenuComponent implements OnInit, OnChanges, OnDestroy {

    @Input() width = '200px';
    @Input() height = '100%';
    @Input() sidebarMenuData = [];
    @Output() subTitle = new EventEmitter<any>();
    selectMenu: any;
    subscribe: any;
    isCollapsed = false;

    constructor(private router: Router,
                private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.router.events.subscribe(event => {
            if (event instanceof NavigationEnd) {
                const currentPath = event.url;
                this.sidebarMenuData.map(i => {
                    i.isOpen = false;
                    i.childMenu.map(child => {
                        child.status = false;
                        if (currentPath.indexOf(child.url) !== -1) {
                            i.isOpen = true;
                            this.selectMenu = i;
                            this.childMenuClick(child.url, child.name);
                        } else {
                            this.handleExceptionUrls(currentPath);
                        }
                    });
                });
            }
        });

        this.handleExceptionUrls(this.route.snapshot['_routerState'].url);
    }

    ngOnDestroy() {

    }

    ngOnChanges(event): void {
        if (event.sidebarMenuData.currentValue) {
            event.sidebarMenuData.currentValue.map(i => {
                if (i.isOpen) {
                    this.selectMenu = i;
                }
            });
            if (this.selectMenu) {
                this.selectMenu.childMenu.map(i => {
                    if (i.status) {
                        this.subTitle.emit(i.name);
                    }
                });
            }
        }
    }

    firstMenuClick(name: string) {
        this.sidebarMenuData.map(i => {
            if (i.name === name) {
                this.selectMenu = i;
                i.isOpen = true;
            } else {
                i.isOpen = false;
            }
        });
    }

    childMenuClick(url: string, name: string) {
        this.selectMenu.childMenu.map(i => {
            i.status = url.indexOf(i.url) !== -1;
            this.subTitle.emit(name);
        });

    }

    /**
     *
     * @param url 处理因为信息报送下添加功能路由设计错误产生的问题
     */
    private handleExceptionUrls(url: string) { //
        if (url.indexOf('?')) {
            const targetPath = url.split('?')[0];
            if (targetPath === '/home/information-submis/add-infromation' || targetPath === '/home/information-submis/edit-information') {
                const type = this.route.snapshot.queryParams.type;
                let targetName = '';
                switch (type) {
                    case '1':
                        targetName = '人工饲草基地种植情况';
                        break;
                    case '2':
                        targetName = '牧草良种繁育基地生产情况';
                        break;
                    case '3':
                        targetName = '饲草种植加工及商品草销售情况';
                        break;
                }
                this.sidebarMenuData.map(i => {
                    if (i.name === '信息报送') {
                        i.isOpen = true;
                        this.selectMenu = i;
                        this.selectMenu.childMenu.map(child => {
                            if (child.name === targetName) {
                                child.status = true;
                                this.subTitle.emit(child.name);
                            } else {
                                child.status = false;
                            }
                        });
                    } else {
                        i.isOpen = false;
                    }
                });
            }
        }
    }

}
